
package shops;

import static java.lang.System.out;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import mytool.Sender;
import org.apache.struts2.ServletActionContext;

public class cartProcess {
    
    private int id;
    private HashSet<CartItem> shoppingCart;
    HashMap<byte[],Object> aa;
    public cartProcess() {
    }
    
    
     
  
    public String add() throws Exception {
        
        
        
         HttpServletRequest request=ServletActionContext.getRequest();         
        
         if(request.getSession().getAttribute("Cart")==null)
             this.setShoppingCart(new HashSet<CartItem>());
         else
             this.setShoppingCart((HashSet<CartItem>)(request.getSession().getAttribute("Cart")));
                
        NWDB.NWDB mydb=new NWDB.NWDB();
        NWDB.IProduct ip=new NWDB.ImplProduct(mydb.getConnection());
        mydb.setIProduct(ip);

        
       if(getId()==0)
       return "success";
       else
       {
        HashMap<String,Object> product=mydb.GetProductCartItem(getId());
        CartItem ci=new CartItem();
        ci.setId(getId());
        ci.setName(product.get("Name").toString());
        ci.setPrice((Double)product.get("Price"));
        ci.setStock((Short)product.get("Stock"));
        ci.setType(product.get("Type").toString());
        ci.setQuantity(1);
        
     
            this.shoppingCart.add(ci);
           
         //儲存購物車內容
            request.getSession().setAttribute("Cart", this.shoppingCart);
      
          
        
        //計算購物車結果資訊
        int shipping = 0;
        double amount = 0;

        for (CartItem c : this.shoppingCart)
            amount += c.getSubTotal();

        if (amount < 1000)
            shipping = 250;
        else if (amount < 3000)
            shipping = 200;

        request.getSession().setAttribute("shipping", shipping);
        request.getSession().setAttribute("amount", amount);
        request.getSession().setAttribute("total", shipping + (int)Math.round(amount));
        
        return "success";
       }
    }
    
    //清空購物車
    public String clear() throws Exception {        
        
         HttpServletRequest request=ServletActionContext.getRequest();
         request.getSession().removeAttribute("Cart");
          request.getSession().removeAttribute("shipping");   
         request.getSession().removeAttribute("amount");   
         request.getSession().removeAttribute("total");  
         return "success";
    }
    
    //結帳
      public String Pay() throws Exception {        
                         
         return "success";
    }
    //訂單產生
    public String finish() throws Exception {        
        HttpServletRequest request=ServletActionContext.getRequest();         
        this.setShoppingCart((HashSet<CartItem>)(request.getSession().getAttribute("Cart")));
         String uname = request.getParameter("uname");
         String uaddress = request.getParameter("uaddress");
         String delivery = request.getParameter("delivery");   
         String memberid = request.getSession().getAttribute("會員編號").toString();
         String uemail = request.getParameter("uemail");   
         String total = request.getSession().getAttribute("total").toString();
        NWDB.NWDB mydb=new NWDB.NWDB();
        NWDB.IProduct ip=new NWDB.ImplProduct(mydb.getConnection());
        mydb.setIProduct(ip);  
      
        int ship=Integer.parseInt(request.getSession().getAttribute("shipping").toString());
        mydb.AddOrders(Integer.parseInt(memberid), ship, uname, uaddress, "112", delivery);
        
        
        for(CartItem ci:this.getShoppingCart())         
        {    
               mydb.AddOrderDetails(Integer.parseInt(memberid), (int)ci.getPrice(), ci.getQuantity(), ci.getId());
               mydb.UpdateStock(ci.getId(), ci.getStock()-ci.getQuantity());
        }
       
         request.getSession().setAttribute("uname", uname);
         request.getSession().setAttribute("uaddress", uaddress);
         request.getSession().setAttribute("delivery", delivery);
         request.getSession().setAttribute("orderId", mydb.GetOrdersId(Integer.parseInt(memberid)));
        
         //寄送EMAIL
      
         
       CartItem shopItem; 
       String ii="";
    
            Iterator it=shoppingCart.iterator();
                while( it.hasNext() ){
                    shopItem = (CartItem)it.next();
                   ii+="<tr><td>"+ shopItem.getName()+" , </td></tr>";
                 }
            String Content = "感謝您的購買!<br>"+"<table border='1'cellspacing='5' cellpadding='5' align='left'><tr align='center'><td>您的商品資訊 : </td></tr>"+ii+"<tr><td>總金額 : "+total+"</td></tr>"+"<tr align='center'><td>您的個人資料 : </td></tr>"+
                    "<tr><td>姓名 :"+uname+"</td></tr>"+
                    "<tr><td>地址 : "+uaddress+"</td></tr>"+
                    "<tr><td>運送方式 :"+delivery+"</td></tr>"+
                    " </table><br>"+"如有任何疑問請回覆此信將您的問題寫下，謝謝!";       
     
         new Sender(uemail,"公仔網感謝您的購買!",Content);   
         
         //扣庫存量
        
         return "success";
    }
    
    //結束購買，清空購物車
    public String end() throws Exception {        
         HttpServletRequest request=ServletActionContext.getRequest();
         request.getSession().removeAttribute("Cart");   
         request.getSession().removeAttribute("shipping");   
         request.getSession().removeAttribute("amount");   
         request.getSession().removeAttribute("total");  
         request.getSession().removeAttribute("uname");  
         request.getSession().removeAttribute("uaddress");  
         request.getSession().removeAttribute("delivery");  
             
         
         return "success";
    }
    //更新購物車
    public String update() throws Exception {        
                
        HttpServletRequest request=ServletActionContext.getRequest();         
        this.setShoppingCart((HashSet<CartItem>)(request.getSession().getAttribute("Cart")));
       
        for(CartItem ci:this.getShoppingCart())         
        {
            if(request.getParameter("q"+ci.getId())!=null)
         ci.setQuantity(Integer.parseInt(request.getParameter("q"+ci.getId())));
            else
                ci.setQuantity(0);
 
        }
       Iterator<CartItem> it = shoppingCart.iterator();  
          while(it.hasNext()){
            if(it.next().getQuantity()==0)
                it.remove();
           }
      
        //儲存購物車內容
        request.getSession().setAttribute("Cart", this.getShoppingCart());

        //計算購物車結果資訊
        int shipping = 0;
        double amount = 0;

        for (CartItem c : this.getShoppingCart())
            amount += c.getSubTotal();

        if (amount < 1000)
            shipping = 250;
        else if (amount < 3000)
            shipping = 200;

        request.getSession().setAttribute("shipping", shipping);
        request.getSession().setAttribute("amount", amount);
        request.getSession().setAttribute("total", shipping + (int)Math.round(amount));
         
         return "success";
    }

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * @return the shoppingCart
     */
    public HashSet<CartItem> getShoppingCart() {
        return shoppingCart;
    }

    /**
     * @param shoppingCart the shoppingCart to set
     */
    public void setShoppingCart(HashSet<CartItem> shoppingCart) {
        this.shoppingCart = shoppingCart;
    }
  

  
}
